package v1

import (
	"com.cyt.common/library/upload"
	"github.com/gogf/gf/frame/g"
)

// 上传API对象
var Upload = new(uploadApi)

type uploadApi struct {
	Controller
}

// 上传文件
func (c *uploadApi) Upload() {
	_, file, err := c.Request.FormFile("file");
	// 根据需求判断文件类型, 文件大小
	if err != nil {
		c.Error("请选择文件上传")
	}
	err = upload.VerifyFile("image", file)
	if err != nil {
		c.Error(err.Error())
	}
	filePath, _, uploadErr := upload.Storage().Upload(file)
	if uploadErr != nil {
		c.Error("文件上传失败:" + uploadErr.Error())
	}
	if filePath == "" {
		c.Error("文件上传错误")
	}
	DomainUrl := g.Cfg().GetString("disks." + g.Cfg().GetString("filesystem.default") + ".Url")
	fileUrl := DomainUrl + filePath
	c.Success("上传成功", fileUrl)
}